//
//  CLCacheCell.m
//  Charles
//
//  Created by Charles on 2017/6/21.
//  Copyright © 2017年 charles. All rights reserved.
//

#import "CLCacheCell.h"
#import "UITableView+FDTemplateLayoutCell.h"
@interface CLCacheCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIImageView *contentImageView;
@property (nonatomic, strong) UILabel *usernameLabel;
@property (nonatomic, strong) UILabel *timeLabel;

@end

@implementation CLCacheCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpView];
    }
    return self;
}

- (void)setUpView {
    //添加约束
    CGFloat padding = 10;
    
    UIView *superView = self.contentView;
    
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:16];//2,9,106
    self.titleLabel.textColor = [UIColor colorWithRed:0 green:0 blue:89 alpha:1];
    self.titleLabel.backgroundColor = [UIColor clearColor];
    self.titleLabel.preferredMaxLayoutWidth = CGRectGetWidth(superView.frame) - padding *2;
    [superView addSubview:self.titleLabel];
    
    self.contentLabel = [[UILabel alloc]init];
    self.contentLabel.font = [UIFont systemFontOfSize:14];
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
    self.contentLabel.backgroundColor = [UIColor clearColor];
    self.contentLabel.preferredMaxLayoutWidth = CGRectGetWidth(superView.frame) - padding *2;
    [superView addSubview:self.contentLabel];

    self.contentImageView = [[UIImageView alloc]init];
    self.contentImageView.backgroundColor = [UIColor clearColor];
    self.contentImageView.contentMode = UIViewContentModeScaleAspectFit;
    [superView addSubview:self.contentImageView];
    
    self.usernameLabel = [[UILabel alloc]init];
    self.usernameLabel.font = [UIFont systemFontOfSize:14];
    self.usernameLabel.backgroundColor = [UIColor clearColor];
    self.usernameLabel.preferredMaxLayoutWidth = CGRectGetWidth(superView.frame)/2;
    [superView addSubview:self.usernameLabel];
    
    self.timeLabel = [[UILabel alloc]init];
    self.timeLabel.font = [UIFont systemFontOfSize:14];
    self.timeLabel.textColor = [UIColor blueColor];
    self.timeLabel.backgroundColor = [UIColor clearColor];
    self.timeLabel.preferredMaxLayoutWidth = CGRectGetWidth(superView.frame)/2;
    [superView addSubview:self.timeLabel];

    MASAttachKeys(self.titleLabel,self.contentLabel,self.contentImageView,self.usernameLabel,self.timeLabel);
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(superView.trailing).with.offset(padding).priority(999);
        make.leading.equalTo(superView.leading).with.offset(padding).priority(999);
        make.top.equalTo(self.contentView.top).offset(padding);
        make.bottom.equalTo(self.contentLabel.top).offset(-padding);
    }];
    
    [self.contentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(superView.trailing).with.offset(-padding).priority(999);
        make.leading.equalTo(self.titleLabel.leading).priority(999);
        make.top.equalTo(self.titleLabel.bottom).offset(padding);
        make.bottom.equalTo(self.contentImageView.top).offset(-padding);
    }];
    
    [self.contentImageView makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.titleLabel.leading).with.priority(999);
        make.trailing.lessThanOrEqualTo(superView.trailing).with.offset(-padding *2).priority(999);
        make.top.equalTo(self.contentLabel.bottom).offset(padding);
        make.bottom.equalTo(self.usernameLabel.top).offset(-padding);
        make.baseline.equalTo(superView.baseline);
    }];
    
    [self.usernameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(superView.bottom).offset(-padding);
        make.leading.equalTo(self.titleLabel.leading);
        make.baseline.equalTo(self.timeLabel.baseline);
    }];
    
    [self.timeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(superView.trailing).offset(padding);
        make.baseline.equalTo(self.usernameLabel.baseline);
    }];
}

- (void)setEntity:(CLEntity *)entity {
    _entity = entity;
    self.titleLabel.text = entity.title;
    self.contentLabel.text = entity.content;
    
    self.contentImageView.image = entity.imageName.length > 0 ? [UIImage imageNamed:entity.imageName] : nil;
    self.usernameLabel.text = entity.username;
    self.timeLabel.text = entity.time;
    
}


// If you are not using auto layout, override this method, enable it by setting
// "fd_enforceFrameLayout" to YES.
- (CGSize)sizeThatFits:(CGSize)size {
    CGFloat totalHeight = 0;
    totalHeight += [self.titleLabel sizeThatFits:size].height;
    totalHeight += [self.contentLabel sizeThatFits:size].height;
    totalHeight += [self.contentImageView sizeThatFits:size].height;
    totalHeight += [self.usernameLabel sizeThatFits:size].height;
    totalHeight += 30; // margins
    return CGSizeMake(size.width, totalHeight);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
